package com.situ.staff.project.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
//import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.staff.project.model.ProjectModel;
import com.situ.staff.project.service.IProjectService;
import com.situ.staff.project.service.Impl.ProjectServiceImpl;
import com.situ.staff.util.FmtRequest;

@WebServlet("/ProjectServlet")
public class ProjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ProjectServiceImpl service = new ProjectServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		Object res = "";
		switch (action) {
		case "page":
			try {
				res = page(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "add":
			try {
				res = add(request);
			} catch (SQLException | ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "del":
			try {
				res = del(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "get":
			try {
				res = get(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "upd":
			try {
				res = upd(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		default:
			break;
		}
		FmtRequest.write(response.getWriter(), res);
	}

	private Object upd(HttpServletRequest request) throws SQLException {
//		ProjectModel model = FmtRequest.parseModel(request, ProjectModel.class);
		String time = request.getParameter("time");
		Timestamp startTime = Timestamp.valueOf(time);
//		System.out.println(startTime);
//		System.out.println(startTime.toString());
		String code = request.getParameter("code");
		String name = request.getParameter("name");
		ProjectModel model = new ProjectModel(code,name,startTime);
		return service.update(model);
	}

	private Object get(HttpServletRequest request) throws SQLException {
		ProjectModel model = FmtRequest.parseModel(request, ProjectModel.class);
		
		return service.selectModel(model);
	}

	private Object del(HttpServletRequest request) throws SQLException {
		ProjectModel model = FmtRequest.parseModel(request, ProjectModel.class);
		return service.delete(model);
	}

	private Object add(HttpServletRequest request) throws SQLException, ParseException {
//		SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		String str = request.getParameter("time");
//		Date date = format.parse(str);
		String time = request.getParameter("time");
		Timestamp startTime = Timestamp.valueOf(time);
//		System.out.println(startTime);
//		System.out.println(startTime.toString());
		String code = request.getParameter("code");
		String name = request.getParameter("name");
		ProjectModel model = new ProjectModel(code,name,startTime);
		return service.insert(model);
	}

	private Object page(HttpServletRequest request) throws SQLException {
		Map<String, String> fields = new HashMap<String, String>();
		fields.put("code", "code");
		fields.put("name", "name");
		// 根据映射关系，从请求中获取实体类对象
		ProjectModel model = FmtRequest.parseModel(request, ProjectModel.class, fields);

		String pageIndex = request.getParameter("pageIndex");
		String pageLimit = request.getParameter("pageLimit");

		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		List<ProjectModel> list = service.selectList(model);

		Integer count = service.selectCount(model);
//		System.out.println(list.toString());
//		System.out.println(count);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("count", count);
		return map;
	}

}
